package com.xhh.nphm.common.config;

import com.xhh.nphm.common.MultiRequestBodyArgumentResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.List;

/**
 * 添加多RequestBody解析器
 * 添加注解@EnableWebMvc，会有如下问题：No converter found for return value of type: class java.util.LinkedHashMap
 * @author ydq
 */
@Configuration
//@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new MultiRequestBodyArgumentResolver());
    }

    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        return new StringHttpMessageConverter(Charset.forName("UTF-8"));
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        super.configureMessageConverters(converters);
        converters.add(responseBodyConverter());
    }

    /**
     * 跨域 配置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")  // 拦截所有的请求
//                .allowedOrigins("*")  // 可跨域的域名
//                .allowCredentials(true)
//                .allowedMethods("*")   // 允许跨域的方法
//                .allowedHeaders("*");  // 允许跨域的请求头
    }
}
